// pages/news/news.js
const encrypt = require("../../utils/encrypt.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    currentPage: 1,
    currentPage2: 1,
    news:[],
    message:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index = app.globalData.newsindex
    
    if (index != null) {
      this.setData({
        index: index
      })
    }
    var that = this;
    let parameter = encrypt.encode(JSON.stringify({ currentPage: this.data.currentPage }));
    
    var data = { parameter: parameter }
    app.request(encrypt.url + 'getCNewsList.ashx', data, function (res) {

      if (res.data.code == 200) {
        let newlist = res.data.data.newsList;
        
        that.setData({
          news: newlist
        })

      }
    })
    var parameter2 = encrypt.encode(JSON.stringify({ currentPage: this.data.currentPage2 }));
    var data = { parameter: parameter2 }
    app.request(encrypt.url + 'getHNewsList.ashx', data, function (res) {

      if (res.data.code == 200) {
        let newlist = res.data.data.newsList;
        
        that.setData({
          message: newlist
        })

      }
    })
  },
  changeTabbar: function (e) {
    this.setData({ index: e.currentTarget.dataset.id })
  },
  newsJump:function(e){ //新闻跳转
    var id = e.currentTarget.dataset.id
    
    wx.navigateTo({
      url: '../newsDetail/newsDetail?id='+id,
    })
  },
  messageJump:function(e){//时讯跳转
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../eventDetail/eventDetail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    
    // 显示加载图标
    
    if (that.data.index == 1){
      wx.showLoading({
        title: '正在加载中',
      })
      that.data.currentPage +=1
      
      var parameter = encrypt.encode(JSON.stringify({ currentPage: that.data.currentPage }));
      
      var data = { parameter: parameter }
      app.request(encrypt.url + 'getCNewsList.ashx', data, function (res) {

        if (res.data.code == 200) {
          let newlist = res.data.data.newsList;
          
          var newArr = that.data.news.concat(newlist)
          
          that.setData({
            news: newArr
          })

        }
        wx.hideLoading();
      })
    } else if (that.data.index == 2) {
      wx.showLoading({
        title: '正在加载中',
      })
      that.data.currentPage2 += 1
      
      var parameter2 = encrypt.encode(JSON.stringify({ currentPage: that.data.currentPage2 }));

      var data = { parameter: parameter2 }
      app.request(encrypt.url + 'getHNewsList.ashx', data, function (res) {

        if (res.data.code == 200) {
          let newlist = res.data.data.newsList;
          
          var newArr2 = that.data.message.concat(newlist)
          
          that.setData({
            message: newArr2
          })

        }
        wx.hideLoading();
      })
    }

    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})